.PHONY: help setup start stop restart status logs deploy test clean dashboard

help:
	@echo "=================================================="
	@echo "📦 Browse ETL 管道管理"
	@echo "=================================================="
	@echo ""
	@echo "🚀 核心命令:"
	@echo "  make start          - 启动所有服务（默认：consumer.py）"
	@echo "  make stop           - 停止所有服务"
	@echo "  make restart        - 重启当前运行的服务"
	@echo "  make status         - 查看服务状态"
	@echo ""
	@echo "🔧 部署命令:"
	@echo "  make deploy         - 部署Lambda和API Gateway"
	@echo ""
	@echo "🧪 测试命令:"
	@echo "  make test           - 自动化测试（所有test-cases）"
	@echo "  make test-simple    - 快速测试（单个事件）"
	@echo ""
	@echo "🔀 处理器模式:"
	@echo "  make start                         - consumer.py 模式（默认，本地开发）"
	@echo "  make start-lambda                  - Lambda 模式（模拟 AWS 生产环境）"
	@echo "  make start-flink-local             - Flink Agents 本地模式（实时处理）"
	@echo "  make start-flink-cluster           - Flink Agents 集群模式（批量处理所有历史数据）"
	@echo "  make start-flink-cluster-streaming - Flink Agents 集群模式（✨流式处理，持续消费新数据）"
	@echo "  make start-flink                   - Flink Agents 本地模式（向后兼容）"
	@echo "  make flink-ui                      - 打开 Flink Web UI (http://localhost:8081)"
	@echo ""
	@echo "📊 监控命令:"
	@echo "  make logs           - 查看所有服务日志"
	@echo "  make logs-ls        - 查看LocalStack日志"
	@echo "  make logs-pg        - 查看PostgreSQL日志"
	@echo "  make logs-event     - 查看Event Processor日志"
	@echo "  make logs-lambda    - 查看Lambda日志（behavior-processor）"
	@echo "  make logs-flink-local   - 查看Flink Agents本地模式日志"
	@echo "  make logs-flink-cluster - 查看Flink Agents集群模式日志（批量）"
	@echo "  make logs-flink-cluster-streaming - 查看Flink Agents集群模式日志（流式）"
	@echo "  make dashboard      - 实时监控面板"
	@echo ""
	@echo "🧹 清理命令:"
	@echo "  make clean          - 完全清理（删除所有数据）"
	@echo ""
	@echo "=================================================="

# 启动服务
start:
	@./scripts/start-all.sh

# 停止服务
stop:
	@./scripts/stop-all.sh

# 重启服务
restart:
	@./scripts/restart-all.sh

# 查看状态
status:
	@./scripts/status.sh

# 部署Lambda
deploy:
	@./scripts/deploy-lambda-docker.sh

# 清理Lambda依赖缓存
clean-lambda-cache:
	@echo "🗑️  清理Lambda依赖缓存..."
	@rm -rf /tmp/browse-etl-lambda-deps
	@echo "✅ 缓存已清理"

# 初始化数据库表结构
init-db:
	@./scripts/init-database.sh

# 自动化测试（所有测试用例）
test:
	@./scripts/test-pipeline.sh

# 快速测试（单个事件）
test-simple:
	@echo "🧪 快速测试..."
	@curl -X POST http://localhost:4566/restapis/$$(docker exec browse-etl-localstack awslocal apigateway get-rest-apis --query 'items[0].id' --output text)/local/_user_request_/events \
	  -H 'Content-Type: application/json' \
	  -d '{"userId":"test_user","eventType":"click","url":"https://example.com","timestamp":'$$(date +%s)'000}' || echo "❌ 测试失败"

# 查看日志
logs:
	@./scripts/logs.sh

logs-ls:
	@./scripts/logs.sh localstack

logs-pg:
	@./scripts/logs.sh postgres

logs-event:
	@./scripts/logs.sh event

# 监控面板
dashboard:
	@./scripts/dashboard.sh

# 清理
clean:
	@./scripts/clean-all.sh

# 查询数据库
query:
	@./scripts/query-postgres.sh

# ===== Lambda 模式相关命令 =====

# 启动 Lambda 模式
start-lambda:
	@./scripts/start-lambda.sh

# 查看 Lambda 日志
logs-lambda:
	@echo "📜 查看 behavior-processor Lambda 日志..."
	@docker exec browse-etl-localstack awslocal logs tail /aws/lambda/behavior-processor --follow || echo "❌ 无法查看日志，请确保 Lambda 已部署"

# 部署 behavior-processor Lambda（单独部署）
deploy-behavior-processor:
	@./scripts/deploy-behavior-processor.sh

# ===== Flink Agents 相关命令 =====

# 启动 Flink Agents 本地模式
start-flink-local:
	@./scripts/start-flink-local.sh

# 启动 Flink Agents 集群模式（批量处理）
start-flink-cluster:
	@./scripts/start-flink-cluster.sh

# 启动 Flink Agents 集群模式（✨流式处理）
start-flink-cluster-streaming:
	@./scripts/start-flink-cluster-streaming.sh

# 向后兼容：start-flink 映射到 start-flink-local
start-flink:
	@./scripts/start-flink-local.sh

# 查看 Flink 日志
logs-flink:
	@./scripts/logs.sh flink

logs-flink-local:
	@docker logs -f browse-etl-flink-agents-processor

logs-flink-cluster:
	@docker logs -f browse-etl-flink-cluster-processor

logs-flink-cluster-streaming:
	@docker logs -f browse-etl-flink-cluster-processor-streaming

logs-flink-agent:
	@docker logs -f browse-etl-flink-agents-processor

logs-flink-jm:
	@docker logs -f browse-etl-flink-jobmanager

logs-flink-tm:
	@docker logs -f browse-etl-flink-taskmanager

# Flink Web UI
flink-ui:
	@echo "🌐 打开 Flink Web UI..."
	@open http://localhost:8081 || xdg-open http://localhost:8081 || echo "请手动打开: http://localhost:8081"

# ===== Java Flink LLM 相关命令 =====

# 启动 Java Flink 集群
start-flink-java:
	@echo "🚀 启动 Java Flink LLM 集群..."
	@cd docker && docker-compose --profile flink-java up -d
	@echo "⏳ 等待 LocalStack 和 Kinesis 就绪..."
	@sleep 15
	@./scripts/deploy-java-flink.sh

# 停止 Java Flink 集群
stop-flink-java:
	@echo "🛑 停止 Java Flink LLM 集群..."
	@cd docker && docker-compose --profile flink-java down

# Java Flink 测试
test-flink-java:
	@echo "🧪 测试 Java Flink LLM 处理器..."
	@FLINK_MODE=java ./scripts/test-pipeline.sh

# Java Flink 日志
logs-flink-java:
	@docker logs -f browse-etl-flink-java-taskmanager

logs-flink-java-jm:
	@docker logs -f browse-etl-flink-java-jobmanager

logs-flink-java-tm:
	@docker logs -f browse-etl-flink-java-taskmanager

# Java Flink Web UI
flink-java-ui:
	@echo "🌐 打开 Java Flink Web UI..."
	@open http://localhost:8082 || xdg-open http://localhost:8082 || echo "请手动打开: http://localhost:8082"

# 查询 Java Flink 处理结果
query-flink-java:
	@echo "📊 查询 Java Flink 处理结果..."
	@docker exec browse-etl-postgres psql -U postgres -d behavior_analysis -c \
	  "SELECT id, user_id, behavior_type, analysis_method, LEFT(behavior_description, 50) as description, created_at FROM user_behaviors WHERE analysis_method LIKE '%java%' OR analysis_method LIKE '%llm%' ORDER BY created_at DESC LIMIT 10;"

